<?php
namespace App\Http\Controllers\Admin;
use App\Models\Node;
/**
 * NodeController.php
 * @author LF
 * @Date 16/7/8 14:28
 * @copyright Copyright© 2016-2016 飞煌科技
 * All Rights Reserved.
 * Code is poetry, thus is life.
 * @author JeffMing
 */
class NodeController extends BaseController{
    private $model;
    public function __construct()
    {
        parent::__construct();
        $this->model = new Node();
    }

    /***
     * 节点列表页
     */
    public function getIndex(){
        $name = request('name');
        $where = [];
        if(!empty($name)){
            $where['name'] = trim($name);
        }
//        $where['pid'] = array('>',0);
        $data = $this->model->getPageData($where,'*',['id' => 'desc'],15);
        if($data){
            $pids = array_unique(array_column($data->toArray()['data'],'pid'));
            $pdata = $this->model->getAllData(['id'=>['in',$pids]]);
            $pdata = changeArray($pdata,'id');
//            dd($pdata);
            foreach($data as $key => $item){
               if(($item['pid']!=0) && isset($pdata[$item['pid']])) {
                   $data[$key]['pname'] = $pdata[$item['pid']]['name'];
               }else{
                   $data[$key]['pname'] = $item['name'];
                   $data[$key]['name']  = "--";
               }
            }
        }
        return view('admin.node.index',compact('data','name'));
    }

    /**
     * 添加节点
     */
    public function getAdd(){
        $pdata = $this->model->getAllData(['pid'=>0]);
        return view('admin.node.form',compact('pdata'));
    }

    public function postAdd(){
        $request  = request()->except('_token');
        $data['pid'] = $request['pnid'];
        $data['name'] = $request['name'];
        $url_arr = explode('/',$request['url']);
        $url = count($url_arr)==3?$request['url'].'/index':$request['url'];
        $data['url'] = $url;
        $data['status'] = $request['status'];
        $data['icon'] = $request['icon'];
        $data['sort'] = $request['sort'];
        $res = $this->model->add($data);
        if($res){
            return redirect(action(NAMESPACE_ADMIN.'NodeController@getIndex'));
        }
        return redirect(action(NAMESPACE_ADMIN.'NodeController@getAdd'));
    }

    /***
     * 修改节点
     */
    public function getEdit($id){
        $node = $this->model->getOne(['id'=>$id]);
        $pdata = $this->model->getAllData(['pid'=>0]);
        return view('admin.node.form',compact('node','pdata'));
    }

    public function postEdit(){
        $request  = request()->except('_token');
        $data['pid'] = $request['pnid'];
        $data['name'] = $request['name'];
        $url_arr = explode('/',$request['url']);
        $url = count($url_arr)==3?$request['url'].'/index':$request['url'];
        $data['url'] = $url;
        $data['status'] = $request['status'];
        $data['icon'] = $request['icon'];
        $data['sort'] = $request['sort'];
        $id = $request['id'];
        $res = $this->model->up($id,$data);
        if($res){
            return redirect(action(NAMESPACE_ADMIN.'NodeController@getIndex'));
        }
        return redirect(action(NAMESPACE_ADMIN.'NodeController@getEdit',['id'=>$id]));
    }

    /**
     * 删除节点
     */
    public function getDel($id){
        $request = request();
        $this->model->del($id);
        return redirect(action(NAMESPACE_ADMIN.'NodeController@getIndex'));
    }
}
